﻿var map, clusterLayer;

function GetMap() {
    var mapOptions =
    {
        credentials: "YOUR_BING_MAPS_KEY",
        zoom: 2
    };

    map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapOptions);

    //Register and load the Point Based Clustering Module
    RegisterModule("PointBasedClusteringModule", "/js/PointBasedClustering.min.js");

    Microsoft.Maps.loadModule("PointBasedClusteringModule", {
        callback: function () {
            clusterLayer = new PointBasedClusteredEntityCollection(map);
        }
    });
}

function RegisterModule(name, url) {
    //Register the module
    Microsoft.Maps.registerModule(name);

    //Load the JavaScript File
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", url);
    document.body.appendChild(script);
}

//Makes a request for data
function RequestData() {
    var size = parseInt(document.getElementById('dataSize').value);
    TestDataGenerator.GenerateData(size, RequestDataCallback);
}

//Handle the data response
function RequestDataCallback(response) {
    if (response != null) {
        clusterLayer.SetData(response);
    }
}

//Initialization logic for loading the map control
(function () {
    function initialize() {
        Microsoft.Maps.loadModule('Microsoft.Maps.Map', { callback: GetMap });
    }

    document.addEventListener("DOMContentLoaded", initialize, false);
})();